package org.linlinjava.litemall.admin.service;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.collections.map.HashedMap;
import org.linlinjava.litemall.admin.dto.MaterialReportVO;
import org.linlinjava.litemall.admin.dto.MaterialVO;
import org.linlinjava.litemall.db.dao.ProductMapper;
import org.linlinjava.litemall.db.domain.Product;
import org.linlinjava.litemall.db.domain.ProductExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Author tony
 * Description
 * Date 2020/10/23 0:21
 */
@Service
public class AdminMaterialService {

    private static Logger logger = LoggerFactory.getLogger(AdminMaterialService.class);


    @Resource
    private ProductMapper productMapper;




    public List<MaterialReportVO> querySelective(Long startTime, Long endTime, String name) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();

        logger.info("querySelective startTime:{},endTime:{}",startTime,endTime);
        if (!ObjectUtils.isEmpty(startTime)&&!ObjectUtils.isEmpty(endTime)) {
            LocalDateTime start = new Date(startTime).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
            LocalDateTime end = new Date(endTime).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
            logger.info("start:{},end:{}",start,end);
            System.out.println("start="+start+"   endTime="+end);
            criteria.andAddTimeBetween(start,end);
        }
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");

        List<MaterialReportVO>  reportVOS = Lists.newArrayList();
        List<Product> products = productMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(products)){
            return reportVOS;
        }
        Map<Integer, BigDecimal> totalAmount = new HashedMap();
        Map<Integer,BigDecimal> totalPriceMap = new HashedMap();
        Map<Integer,BigDecimal> priceMap = new HashedMap();
        Map<Integer,String> units = new HashedMap();
        Map<Integer,String> names = new HashedMap();

        for (Product product : products) {
            String material = product.getMaterial();
            List<MaterialVO> materialVOS = new Gson().fromJson(material, new TypeToken<List<MaterialVO>>() {
            }.getType());
            for (MaterialVO materialVO : materialVOS) {
                Integer id = materialVO.getId();
                String name1 = materialVO.getName();
                String unit = materialVO.getUnit();
                BigDecimal amount = materialVO.getAmount();
                BigDecimal price = materialVO.getPrice();
                BigDecimal totalPrice = materialVO.getTotalPrice();
                if(!units.containsKey(id)){
                    units.put(id,unit);
                }
                if(!names.containsKey(id)){
                    names.put(id,name1);
                }
                if(!priceMap.containsKey(id)){
                    priceMap.put(id,price);
                }
                if(!totalAmount.containsKey(id)){
                    totalAmount.put(id,amount);
                }else{
                    totalAmount.put(id,totalAmount.get(id).add(amount));
                }
                if(!totalPriceMap.containsKey(id)){
                    totalPriceMap.put(id,totalPrice);
                }else{
                    totalPriceMap.put(id,totalPriceMap.get(id).add(totalPrice));
                }

            }
        }

        List<MaterialReportVO> reportVOList =new ArrayList<>();
        for (Map.Entry<Integer, String> entry : names.entrySet()) {
            Integer id = entry.getKey();
            String materailnName = entry.getValue();
            String unitname = units.get(id);
            BigDecimal totalPricevo=totalPriceMap.get(id);
            BigDecimal totalAmountvo = totalAmount.get(id);
            MaterialReportVO materialReportVO = new MaterialReportVO(id,materailnName,unitname,totalAmountvo,priceMap.get(id), totalPricevo);
            reportVOS.add(materialReportVO);
            if(name!=null){
                if(materailnName.contains(name)){
                    reportVOList.add(materialReportVO);
                }
            }
        }
        if(name!=null){
            return reportVOList;
        }
        return reportVOS;


    }
}
